//
//  SecondViewController.m
//  gradientlayer
//
//  Created by Damon on 16/3/5.
//  Copyright © 2016年 zmj27404. All rights reserved.
//

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(10, 20, 100, 40);
    [btn setTitle:@"上一页" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(backPage:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:btn];
    [self createImage];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self drawImage];
}

- (void)backPage:(UIButton *)btn {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)createImage {
    CGRect frame = CGRectMake(10, 60, 100, 50);
    UIImageView *img = [[UIImageView alloc] initWithFrame:frame];
    [self.view addSubview:img];
    UIGraphicsBeginImageContextWithOptions(frame.size, NO, [UIScreen mainScreen].scale);
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:img.bounds cornerRadius:5.0];
    [[UIColor blackColor] setFill];
    [path fill];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    img.image = image;
}

static NSUInteger LABCOUNT = 6;
static NSUInteger VERTICALCOUNT = 5;
- (void)drawImage {
    CGRect innerRect = CGRectMake(10, 70, 300, 500);
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:innerRect];
    [self.view addSubview:imgView];
    CGFloat yspace = CGRectGetHeight(innerRect) / (LABCOUNT - 1);
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        UIGraphicsBeginImageContext(innerRect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:imgView.bounds];
//        [[UIColor greenColor] setFill];
        [[UIColor colorWithRed:5/255.0 green:5/255.0 blue:5/255.0 alpha:1.f] setFill];
        [path fill];
//        CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
        //水平
        UIBezierPath *path1 = [UIBezierPath bezierPath];
        for (NSInteger i = 0; i < LABCOUNT - 2; i++) {
            CGFloat y = (i + 1) * yspace;
            [path1 moveToPoint:CGPointMake(0, y)];
            [path1 addLineToPoint:CGPointMake(innerRect.size.width, y)];
        }
        path1.lineWidth = 2.0f;
        [[UIColor whiteColor] setStroke];
        [path1 stroke];
        //垂直
        UIBezierPath *path2 = [UIBezierPath bezierPath];
        CGFloat xspace = CGRectGetWidth(innerRect) / (VERTICALCOUNT - 1);
        for (NSInteger i = 0; i < VERTICALCOUNT - 2; i++) {
            CGFloat x = (i + 1) * xspace;
            [path2 moveToPoint:CGPointMake(x, 0)];
            [path2 addLineToPoint:CGPointMake(x, innerRect.size.height)];
        }
        path2.lineWidth = 1.0f;
        [[UIColor grayColor] setStroke];
        [path2 stroke];
        CGContextAddPath(context, path2.CGPath);
        CGContextAddPath(context, path1.CGPath);
        CGContextDrawPath(context, kCGPathFillStroke);
        CGContextStrokePath(context);
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        dispatch_async(dispatch_get_main_queue(), ^{
            imgView.image = image;
        });
    });
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
